package com.zhang.crowd.mvc.handler;

import com.zhang.crowd.entity.Admin;
import com.zhang.crowd.service.api.AdminService;
import com.zhang.crowd.util.CrowdUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
public class TestHandler {

    @Autowired
    private AdminService adminService;

    private Logger logger = LoggerFactory.getLogger(TestHandler.class);

    @RequestMapping("/")
    public String index(){
        return "admin-login";
    }

    @ResponseBody
    @RequestMapping("/test/async/ajax.html")
    public String test() throws InterruptedException {
        Thread.sleep(5000);
        return "success";
    }

    @RequestMapping("/test/ssm.html")
    public String testSSM(ModelMap modelMap, HttpServletRequest request){
        boolean judgeResult = CrowdUtil.judgeRequestType(request);
        logger.info("judgeResult="+judgeResult);

        List<Admin> adminList = adminService.getAll();
        modelMap.addAttribute("adminList",adminList);
        System.out.println(10/0);
        return "target";
    }

    @ResponseBody
    @RequestMapping("/send/array/one.html")
    //由于jquery在进行发送请求时，把array解析成了array[]所以我们这里也必须用array[]
    public String testReceiveArrayOne(@RequestParam("array[]") List<Integer> array){
        for(Integer number : array){
            System.out.println("Number===>"+number);
        }
        return "target";
    }

}
